/*
 * Created on Oct 9, 2003
 * CategoryDB
 * Local Projects Database
 * (c) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.OracleDBConnector;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.db.PostgresDBConnector;
import ro.gateway.aida.servlet.Constants;


/**
 * @author Mihai Postelnicu
 * CategoryDB
 *
 *  *
 */
public class TestDB extends DBPersistenceManager {
	private TestDB( PersistenceToken token )
			{
				  super( token );
			}

			public static TestDB getManager( PersistenceToken token )
			{
				  return new TestDB( token );
			}


			public boolean isDBConnectionValid() {
			    Connection con = null;
	        try {

	            con = getConnection();

                String TEST_QUERY = null;
                if (Constants.DBC1 instanceof PostgresDBConnector)
                    TEST_QUERY = TEST_POSTGRES;

                if (Constants.DBC1 instanceof OracleDBConnector)
                    TEST_QUERY = TEST_ORACLE;

                PreparedStatement ps = con.prepareStatement(TEST_QUERY);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    int i = rs.getInt(1);
                }
            } catch (Exception e) {
                return false;
            } finally {
                try {
                if (con!=null) con.close(); 
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
			    return true;
			}
			
			public static final String TEST_ORACLE="SELECT 1 FROM dual";
			public static final String TEST_POSTGRES="SELECT 1";
			
}
